/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseHibernateDao;
import javacommon.util.DateTools;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.pub.model.Meeting;
import com.tech15.pub.model.MeetingUser;
import com.tech15.pub.vo.query.MeetingQuery;

@Repository
public class MeetingDao extends BaseHibernateDao<Meeting,Long>{

	public Class getEntityClass() {
		return Meeting.class;
	}
	
	public Page findPage(MeetingQuery query) {
		String sql = "select t.*,t1.realname as username,t2.name as dataname from Meeting t " +
				" left join sys_user t1 on t.ydr=t1.userid " +
				" left join pub_data t2 on t.mrid=t2.id where 1=1 ";
				if(ObjectUtils.isEmpty(query.getHyrqBegin())){
					sql += " and t.hyrq >= date '"+DateTools.getDisplayDate()+"'";
				}
			sql += "/~ and t.zt like '%[zt]%' ~/"
				+ "/~ and to_char(t.hyrq,'yyyy-mm-dd') = '[hyrqBegin]' ~/"
				+ "/~ and t.mrid = '[mrid]' ~/"
				+ "/~ and t.hysj = '[hysj]' ~/"
				+ "/~ and t.ydr = '[ydr]' ~/"
				+ "/~ and t.bz = '[bz]' ~/";
			sql += " order by t.hyrq asc,get_hysj(t.hysj) asc ";
				//+ "/~ order by [sortColumns] ~/";
		Map map = new HashMap();
		map.put("id", "Long");
		map.put("ydr", "Long");
		map.put("zt", "String");
		map.put("hyrq", "Timestamp");
		map.put("dataname", "String");
		map.put("username", "String");
		map.put("hysj", "String");
		map.put("bz", "String");
		return pageSqlQuery(sql, query, map, Meeting.class);	
	}

	public List<Meeting> findMetting(MeetingQuery query) {
		String sql = "select t.*,t1.username,t1.realname from Meeting t left join sys_user t1 on t.ydr=t1.userid where 1=1 "
			+ "/~ and t.zt = '[zt]' ~/"
			+ "/~ and to_char(t.hyrq,'yyyy-mm-dd') = '[hyrqBegin]' ~/"
			+ "/~ and t.mrid = '[mrid]' ~/"
			+ "/~ and t.hysj = '[hysj]' ~/"
			+ "/~ and t.ydr = '[ydr]' ~/"
			+ "/~ and t.bz = '[bz]' ~/"
			+ "/~ order by [sortColumns] ~/";
		
		Map map = new HashMap();
		map.put("zt", "String");
		map.put("username", "String");
		map.put("hysj", "String");
		map.put("realname", "String");
		return findSqlAll(sql, query, map, Meeting.class);
	}

	public void deleteByNotInid(String userid) {
		String sql="delete from meeting_user where userid not in ("+userid+")";
		this.delete(sql);
		
	}

	public Page searchMyMeeting(MeetingQuery query) {
//		String sql="select t.* from meeting t where t.ydr='"+String.valueOf(query.getUserid())+"' or t.id in (select mid from meeting_user where userid='"+String.valueOf(query.getUserid())+"')";
		String sql = "select t.*,t1.realname as username,t2.name as dataname from Meeting t " +
					" left join sys_user t1 on t.ydr=t1.userid " +
					" left join pub_data t2 on t.mrid=t2.id where 1=1 ";
					if(ObjectUtils.isEmpty(query.getHyrqBegin())){
						sql += " and t.hyrq >= date '"+DateTools.getDisplayDate()+"'";
					}
				sql += "/~ and t.zt like '%[zt]%' ~/"
					+ "/~ and to_char(t.hyrq,'yyyy-mm-dd') = '[hyrqBegin]' ~/"
					+ "/~ and t.mrid = '[mrid]' ~/"
					+ "/~ and t.hysj = '[hysj]' ~/"
					+ "/~ and t.ydr = '[ydr]' ~/"
					+ "/~ and t.bz = '[bz]' ~/";
				
				sql+="  and t.ydr='"+String.valueOf(query.getUserid())+"' or t.id in (select mid from meeting_user where userid='"+String.valueOf(query.getUserid())+"')";
				sql += " order by t.hyrq asc,get_hysj(t.hysj) asc ";
					//+ "/~ order by [sortColumns] ~/";
			Map map = new HashMap();
			map.put("id", "Long");
			map.put("ydr", "Long");
			map.put("zt", "String");
			map.put("hyrq", "Timestamp");
			map.put("dataname", "String");
			map.put("username", "String");
			map.put("hysj", "String");
			map.put("bz", "String");
			return pageSqlQuery(sql, query, map, Meeting.class);
	}

	

}
